package com.cj.sys.web.security;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.cj.common.orm.Page;
import com.cj.common.orm.PropertyFilter;
import com.cj.common.orm.hibernate.EntityManager;
import com.cj.common.orm.hibernate.HibernateWebUtils;
import com.cj.common.web.struts2.CRUDActionSupport;
import com.cj.common.web.struts2.Struts2Utils;
import com.cj.sys.entity.security.Resource;
import com.cj.sys.service.security.ResourceManager;
import com.cj.sys.service.security.UserManager;


@SuppressWarnings("serial")
public class MenuResourceAction extends CRUDActionSupport<Resource> {
	@Autowired
	private ResourceManager resourceManager;
	@Autowired
	private UserManager userManager;
	private final Long rootId = 1L;
	private String menuUrl;
	private Resource oldParent  ;

	public void getMenuTreeXml() throws Exception {
		String xml;
		if (getId() == null) {
			xml = resourceManager.getMenuXmlTree(userManager.getCurrentUser().getId(),rootId);
		} else {
			xml = resourceManager.getMenuXmlTree(userManager.getCurrentUser().getId(),getId());
		}
		Struts2Utils.renderXml(xml);
	}
	
	public void getResourceTree() throws Exception{
		String xml;
		if (getId() == null) {
			xml = resourceManager.getMenuXmlTree(userManager.getCurrentUser().getId(),rootId);
		} else {
			xml = resourceManager.getMenuXmlTree(userManager.getCurrentUser().getId(),getId());
		}
		Struts2Utils.renderXml(xml);
	}
	
	public String menuValue() throws Exception {
		prepareModel();
		menuUrl = entity.getValue();
		return "menu";
	}
	@Override
	public String execute() {
		return SUCCESS;
	}
	@Override
	public String input() {
		try {
			return INPUT;
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}

	@Override
	public String list(){
		try {
			HttpServletRequest request = Struts2Utils.getRequest();
			List<PropertyFilter> filters = HibernateWebUtils
					.buildPropertyFilters(request);
			getPage().setOrderBy("orderNum,resourceType");
			getPage().setOrder(Page.ASC + "," + Page.ASC);
			setPage(resourceManager.search(getPage(), filters));
			return "list";
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}

	@Override
	protected void prepareModel() throws Exception {
		if (getId() != null) {
			entity = resourceManager.get(getId());
		} else {
			entity = new Resource();
		}
	}
	
	@Override
	public void prepareSave() throws Exception { // 用prepareModel修改时会出错
		if (getId() != null) {
			entity = resourceManager.get(getId());
			oldParent = entity.getParent();
			entity.setParent(null);
		} else {
			entity = new Resource();
		}
	}


	@Override
	public EntityManager getEntityManager() {
		return resourceManager;
	}

	public String getMenuUrl() {
		return menuUrl;
	}

	public void setMenuUrl(String menuUrl) {
		this.menuUrl = menuUrl;
	}
	
}
